﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Framework.Controls;
using PlanningGIS.Framework;
using DevComponents.DotNetBar;

using PlanningGIS.Engine.Controls;
using PlanningGIS.Win.Controls;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;

namespace PlanningGIS.CoordCovert.form
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmVectorProject : Office2007Form
    {
        /// <summary>
        /// 
        /// </summary>
        ISpatialReference m_pSpatialRef;
        
        /// <summary>
        /// 
        /// </summary>
        private bool IsFeatureDs = true;

        /// <summary>
        /// 
        /// </summary>
        public FrmVectorProject()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                FrameworkHelper.ShowLogPanel();
                if (txtInputDataset.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输入要素类或者要素集不能为空！");
                    txtInputDataset.Focus();
                    return;
                }

                if (txtInputCoordinate.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输入坐标系统不能为空，否则不能进行投影变换！");
                    txtInputCoordinate.Focus();
                    return;
                }

                if (txtOutputDataset.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输出要素类或者要素集不能为空！");
                    txtOutputDataset.Focus();
                    return;
                }

                if (txtOutputCoordinate.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输出坐标系统不能为空！");
                    txtOutputCoordinate.Focus();
                    return;
                }

                if (txtOutputCoordinate.Text == "Unknown")
                {
                    MsgHelper.ShowInfomationMsgbox("输出坐标系统不能为Unknown！");
                    txtOutputCoordinate.Focus();
                    return;
                }

                this.Cursor = Cursors.WaitCursor;
                Geoprocessor gp = new Geoprocessor();
                IGPProcess process = null;

                LogHelper.WriteLog("正在进行数据投影...");
                if (IsFeatureDs)
                {
                    Project project = new Project();
                    project.in_dataset = this.txtInputDataset.Text;
                    project.out_dataset = this.txtOutputDataset.Text;
                    project.out_coor_system = m_pSpatialRef;
                    process = project;
                }
                else
                {
                    ProjectRaster project = new ProjectRaster();
                    project.in_raster = this.txtInputDataset.Text;
                    project.out_raster = this.txtOutputDataset.Text;
                    project.out_coor_system = m_pSpatialRef;
                    process = project;
                }
                
                string strMessage;
                if (GeoprocessorHelper.Execute(process, out strMessage))
                {
                    strMessage += "投影成功！\r\n";
                    LogHelper.WriteLog(strMessage);
                }
                else
                {
                    strMessage += "投影失败！\r\n";
                    LogHelper.WriteErrLog(strMessage);
                }
                
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenDataset_Click(object sender, EventArgs e)
        {
            OpenDataDialog m_frmAddData1 = new OpenDataDialog();
            m_frmAddData1.MultiSelect=false;
            if (m_frmAddData1.ShowDialog(this) != DialogResult.OK) return;
            List<IDataset> allDatasets = m_frmAddData1.GetAllDatasets();
            if (allDatasets.Count == 0)
            {
                MsgHelper.ShowWarningMsgbox("请选择要素类或者要素集！");
                return;
            }
            foreach (IDataset dataset in allDatasets)
            {
                if (dataset.Type == esriDatasetType.esriDTFeatureClass || dataset.Type == esriDatasetType.esriDTFeatureDataset)
                {
                    IGeoDataset pGeoDataset = dataset as IGeoDataset;
                    txtInputCoordinate.Text = pGeoDataset.SpatialReference.Name;
                    IsFeatureDs = true;
                }
                else if (dataset.Type == esriDatasetType.esriDTRasterDataset)
                {
                    IGeoDataset pGeoDataset = dataset as IGeoDataset;
                    txtInputCoordinate.Text = pGeoDataset.SpatialReference.Name;
                    IsFeatureDs = false;
                }
                else
                {
                    MsgHelper.ShowInfomationMsgbox("请选择要素类或者要素集！");
                    return;
                }
            }
            txtInputDataset.Text = m_frmAddData1.FullName;
            txtOutputDataset.Text = txtInputDataset.Text + "_Project";
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCoordinate_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Title = "选择空间参考文件";
            openDlg.Filter = "坐标文件|*.prj";
            openDlg.Multiselect = false;
            if (openDlg.ShowDialog(this) == DialogResult.OK)
            {
                m_pSpatialRef = GetSpatialReferenceFromPrj(openDlg.FileName.Trim());
                txtOutputCoordinate.Text = m_pSpatialRef.Name;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strPrjName"></param>
        /// <returns></returns>
        private ISpatialReference GetSpatialReferenceFromPrj(string strPrjName)
        {
            if (strPrjName.Trim().Length == 0) return null;
            ISpatialReference pSR = null;
            ISpatialReferenceFactory pSRFactory = new SpatialReferenceEnvironmentClass();
            try
            {
                pSR = pSRFactory.CreateESRISpatialReferenceFromPRJFile(strPrjName);
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("空间坐标参考文件已经破坏或者不支持，请检查：" + ex.Message);
                return null;
            }
            return pSR;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveDataset_Click(object sender, EventArgs e)
        {
            if (IsFeatureDs)
            {
                OpenDataDialog m_frmAddData1 = new OpenDataDialog();
                m_frmAddData1.Text = "保存地址";
                m_frmAddData1.MultiSelect=false;
                if (m_frmAddData1.ShowDialog(this) != DialogResult.OK) return;
                List<IDataset> allDatasets = m_frmAddData1.GetAllDatasets();
                foreach (IDataset dataset in allDatasets)
                {
                    if (dataset.Type == esriDatasetType.esriDTRasterCatalog)
                    {
                        MsgHelper.ShowInfomationMsgbox("请选择矢量要素类或者矢量要素集！");
                        return;
                    }
                }
                txtOutputDataset.Text = m_frmAddData1.FullName;
            }
            else
            {
                SaveFileDialog openFileDialog1 = new SaveFileDialog();
                openFileDialog1.Title = "选择输出栅格数据集文件";
                openFileDialog1.Filter = "IMG文件 (*.img)|*.img|TIF文件 (*.tif)|*.tif|所有文件 (*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string strFile = openFileDialog1.FileName;
                    if (strFile.ToLower().EndsWith(".img") || strFile.ToLower().EndsWith(".tif"))
                    {
                        txtOutputDataset.Text = strFile;
                    }
                }
            }
        }
    }
}
